import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:jtsell_app/entity/cart_item_entity.dart';
import 'package:jtsell_app/entity/goods_detail_entity.dart';
import 'package:jtsell_app/network/http_dispense.dart';
import 'package:jtsell_app/provider/cart_list_model.dart';
import 'package:jtsell_app/provider/index_page_model.dart';
import 'package:jtsell_app/provider/main_category_model.dart';
import 'package:provider/provider.dart';
import 'package:fluwx/fluwx.dart' as fluwx;

class GoodsCart extends StatelessWidget {
  final double widgetHeight = 120;
  final GoodsDetailEntity goodsDetailEntity;

  GoodsCart(this.goodsDetailEntity);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: ScreenUtil().setHeight(widgetHeight),
      width: ScreenUtil().setWidth(1080),
      color: Colors.white,
      child: Row(
        children: <Widget>[
          Container(
            height: ScreenUtil().setHeight(120),
            width: ScreenUtil().setWidth(220),
            child: Consumer<CartListModel>(
              builder: (BuildContext context, CartListModel model, Widget child) {
                return Stack(
                  children: <Widget>[
                    InkWell(
                      onTap: () {
                        Navigator.pop(context);
                        Provider.of<IndexPageModel>(context).setCurrentPageIndex(2);
                      },
                      child: Container(
                        width: ScreenUtil().setWidth(110),
                        alignment: Alignment.center,
                        child: Icon(
                          Icons.shopping_cart,
                          size: 35,
                          color: Colors.red,
                        ),
                      ),
                    ),
                    Positioned(
                      top: 0,
                      right: 10,
                      child: Container(
                        padding: EdgeInsets.fromLTRB(6, 3, 6, 3),
                        decoration: BoxDecoration(
                            color: Colors.pink,
                            border: Border.all(width: 2, color: Colors.white),
                            borderRadius: BorderRadius.circular(12.0)),
                        child: Text(
                          '${model.totalCount}',
                          style: TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(22)),
                        ),
                      ),
                    )
                  ],
                );
              },
            ),
          ),
          Expanded(
            child: InkWell(
              child: Container(
                height: ScreenUtil().setHeight(widgetHeight),
                color: Colors.green,
                child: Center(
                  child: Text(
                    "加入购物车",
                    style: TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(45)),
                  ),
                ),
              ),
              onTap: () {
                GoodsDetailGoodinfo info = goodsDetailEntity.goodInfo;

                CartItemEntity cartItem = CartItemEntity()
                  ..imageUrl = info.comPic
                  ..isSelected = true
                  ..isDeleted = false
                  ..buyCount = 1
                  ..buyLimit = info.amount
                  ..productName = info.goodsName
                  ..price = info.presentPrice
                  ..productID = info.goodsId;

                Provider.of<CartListModel>(context).addItem(cartItem);
              },
            ),
          ),
          Expanded(
            child: InkWell(
              child: Container(
                height: ScreenUtil().setHeight(widgetHeight),
                color: Colors.red,
                child: Center(
                  child: Text(
                    "立即抢购",
                    style: TextStyle(color: Colors.white, fontSize: ScreenUtil().setSp(45)),
                  ),
                ),
              ),
              onTap: () {
                fluwx.responseFromPayment.listen(( data) {
                  print("支付结果---->${data.errCode}");
                });


                HttpDispense.getWechatInfo().then((wechatPayEntity) {
                  if(wechatPayEntity==null){
                    return;
                  }


//                  fluwx.pay(
//                    appId: wechatPayEntity.appid,
//                    partnerId: wechatPayEntity.partnerid,
//                    prepayId: wechatPayEntity.prepayid,
//                    packageValue: wechatPayEntity.package,
//                    nonceStr: wechatPayEntity.noncestr,
//                    timeStamp: wechatPayEntity.timestamp,
//                    sign: wechatPayEntity.sign,
//                  ).then((data) {
//                    print("---》$data");
//                  });
                });
              },
            ),
          )
        ],
      ),
    );
  }
}
